//
//  DifficultController.m
//  AntsExpress
//
//  Created by slccper on 16/1/12.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "DifficultController.h"
#import "clsAntsService.h"
#import "clsOtherFun.h"
#import "clsDifficult.h"
@interface DifficultController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,clsAntsServiceDelegate>
{
    NSMutableArray *_tableList;
    UILabel *number;
    UILabel *name;
    UITextView *dec;
    clsAntsService *ants;
    UIButton *footer;
    UIView *view;
}
@end

@implementation DifficultController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIControl *bgview = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 240)];
    bgview.backgroundColor = [UIColor colorWithRed:246/255.0f green:89/255.0f blue:53/255.0f alpha:1];
//    [self.view addSubview:bgview];
    UILabel *lbnumber = [[UILabel alloc]initWithFrame:CGRectMake(70, 20, 65, 19)];
    lbnumber.text=@"會員編號:";
    lbnumber.textColor = [UIColor whiteColor];
    lbnumber.font = [UIFont systemFontOfSize:13];
    lbnumber.backgroundColor = [UIColor clearColor];
    [bgview addSubview:lbnumber];
    
    number = [[UILabel alloc]initWithFrame:CGRectMake(135, 20, 100, 19)];
    if ([[clsOtherFun getMemberId] integerValue]!=0) {
        number.text = [NSString stringWithFormat:@"%ld",(long)[[clsOtherFun getMemberId] integerValue]+600000];
    }
//    number.text=[NSString stringWithFormat:@"%ld",[[clsOtherFun getMemberId]integerValue]];
    number.textColor = [UIColor whiteColor];
    number.font = [UIFont systemFontOfSize:13];
    number.backgroundColor = [UIColor clearColor];
    [bgview addSubview:number];
    
    
    UILabel *lbname = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-130, 20, 52, 19)];
    lbname.text=@"收件人:";
    lbname.textColor = [UIColor whiteColor];
    lbname.font = [UIFont systemFontOfSize:13];
    lbname.backgroundColor = [UIColor clearColor];
    [bgview addSubview:lbname];
    
    name = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-78, 20, 78, 19)];
    name.text=[clsOtherFun getMemberName];
    name.textColor = [UIColor whiteColor];
    name.font = [UIFont systemFontOfSize:13];
    name.backgroundColor = [UIColor clearColor];
    [bgview addSubview:name];

    dec = [[UITextView alloc]initWithFrame:CGRectMake(15, 50, ScreenWidth-30, 90)];
    dec.textColor = [UIColor whiteColor];
    dec.editable=NO;
    dec.text = @"若果你的快递己顯示簽收，但還沒有在APP里顯示，可能是因為底單模糊／賣家沒有寫／賣家寫錯等等。 請使用搜尋功能並綁回欵難件等。多謝";
    dec.backgroundColor=[UIColor clearColor];
    dec.scrollsToTop=NO;
    [bgview addSubview:dec];
    UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 160, ScreenWidth, 50)];
    search.placeholder = @"單號搜索";
    search.delegate=self;
    search.returnKeyType = UIReturnKeySearch;
    search.showsCancelButton=YES;
    [bgview addSubview:search];
    UIView *allLabel = [[UIView alloc]initWithFrame:CGRectMake(0, 210, ScreenWidth, 30)];
    allLabel.backgroundColor = [UIColor whiteColor];
    //    allLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [allLabel addSubview:[self CreatByText:@"運單號" :0]];
//    [allLabel addSubview:[self CreatByText:@"狀態" :1]];
    [allLabel addSubview:[self CreatByText:@"收件日" :2]];
//    [allLabel addSubview:[self CreatByText:@"到期日" :3]];
    [allLabel addSubview:[self CreatByText:@"費用" :4]];
    [clsOtherFun addAllLayoutConstraints:allLabel];
    [bgview addSubview:allLabel];
    self.tableView.tableHeaderView = bgview;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 190, ScreenWidth, ScreenHeight-190)];
    self.tableView.backgroundColor = [UIColor colorWithRed:208/255.0f green:208/255.0f blue:211/255.0f alpha:1];
    _tableList = [NSMutableArray array];
//    for (int i=0;i<3; i++) {
//        clsCategory *cls = [[clsCategory alloc]init];
//        cls.Waybill=@"22011283";
//        cls.Status=@"可取货";
//        cls.Recei=@"2015.12.29";
//        cls.Date=@"2016.01.03";
//        cls.Freight=@"15";
//        [cls.subCategorys addObject:@""];
//        [_tableList addObject:cls];
//    }
    [self.tableView reloadData];
    UIImageView * icon=[[UIImageView alloc] initWithFrame:CGRectMake(10.0, 5, 30, 30.0)];
    icon.image=[UIImage imageNamed:@"back.png"];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 20, 50, 44);
//    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [button addSubview:icon];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setLeftBarButtonItem:left animated:YES];
    self.title=@"疑難件認領";
    ants = [[clsAntsService alloc]init];
    ants.delegate=self;
    [self getWebData];
    [bgview addTarget:self action:@selector(Cancell) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view from its nib.
}
-(void)getWebData
{
    if ([clsOtherFun CheckNetworkStatus:YES]) {
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        [ants GetProblemExpressList];
    }
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self Cancell];
    searchBar.text=@"";
    [self getWebData];
}
-(void)AddFooterView
{
    if(!footer)
    {
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        footer = [UIButton buttonWithType:UIButtonTypeCustom];
        [footer setTitle:@"加入到我的賬戶" forState:UIControlStateNormal];
        [footer setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        footer.layer.borderColor=[UIColor blackColor].CGColor;
        footer.layer.borderWidth=1;
        footer.layer.cornerRadius=10;
        [footer setFrame:CGRectMake(30, 0, ScreenWidth-60, 50)];
        footer.backgroundColor = [UIColor whiteColor];
        [footer addTarget:self action:@selector(AddTOAccount:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(40, 1, 48, 48)];
        img.image = [UIImage imageNamed:@"jia.jpg"];
        [footer addSubview:img];
        [view addSubview:footer];
        self.tableView.tableFooterView=view;
    }
}
-(void)AddTOAccount:(UIButton *)sender
{
    if ([[clsOtherFun getencodeName]isEqualToString:@""]) {
        [clsOtherFun ShowMsg:@"請先登錄"];
        return;
    }
    clsDifficult *diffcult = [_tableList firstObject];
    [ants ConvertToMyAccount:diffcult.Oid :[clsOtherFun getencodeName]];
}
- (void)Cancell
{
    [self.view endEditing:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.view endEditing:YES];
    [ants GetProbleExpressByid:searchBar.text];
}

#pragma mark - Nested Tables methods
- (NSInteger)mainTable:(UITableView *)mainTable numberOfItemsInSection:(NSInteger)section {
    return [_tableList count];
}

- (NSInteger)mainTable:(UITableView *)mainTable numberOfSubItemsforItem:(SDGroupCell *)item atIndexPath:(NSIndexPath *)indexPath {
//    clsCategory *category = [_tableList objectAtIndex:indexPath.row];
    return 1;
}

- (SDGroupCell *)mainTable:(UITableView *)mainTable setItem:(SDGroupCell *)item forRowAtIndexPath:(NSIndexPath *)indexPath {
    clsDifficult *category = [_tableList objectAtIndex:indexPath.row];
    [item FillData:category];
    //    item.itemText.text = category.CategoryName;
    
    
    return item;
}

- (SDSubCell *)item:(SDGroupCell *)item setSubItem:(SDSubCell *)subItem forRowAtIndexPath:(NSIndexPath *)indexPath {
    clsDifficult *category = [_tableList objectAtIndex:item.cellIndex];
//    clsCategory *subcategory = [category.subCategorys objectAtIndex:indexPath.row];
    [subItem setSubData:category];
    //    subItem set
    return subItem;
}

- (void) mainTable:(UITableView *)mainTable itemDidChange:(SDGroupCell *)item {
}

- (void) mainItem:(SDGroupCell *)item subItemDidChange: (SDSubCell *)subItem forTap:(BOOL)tapped {
    
}

-(UILabel *)CreatByText:(NSString *)text :(NSUInteger)tag
{
    UILabel *status = [[UILabel alloc]initWithFrame:CGRectZero];
    status.text=text;
    status.tag=tag;
    status.textAlignment= NSTextAlignmentCenter;
    status.translatesAutoresizingMaskIntoConstraints = NO;
    
    //    CGSize size = [status.text boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    //    [status setFrame:CGRectMake(0, 0, size.width, 30)];
    status.font = [UIFont systemFontOfSize:15];
    status.textColor=[UIColor blackColor];
    return status;
}
-(void)getAntsDataFail:(NSInteger)type
{
    [clsOtherFun hideLoadingView];
}
-(void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    if (type==Ants_GetProbleExpressByid) {
        NSDictionary *dic =[pDictionary objectForKey:@"data"];
        NSString *status = [dic objectForKey:@"status"];
        if ([status integerValue]==200) {
            clsDifficult *difficult = [[clsDifficult alloc]initWithDic:[dic objectForKey:@"data"]];
            [_tableList removeAllObjects];
            [_tableList addObject:difficult];
            [self AddFooterView];
            [self.tableView reloadData];
        }else{
            [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
        }
    }else if (type==Ants_GetProblemExpressList)
    {
        [clsOtherFun hideLoadingView];
        NSDictionary *dic =[pDictionary objectForKey:@"data"];
        _tableList = [clsDifficult GetDataByArr:[dic objectForKey:@"data"]];
        self.tableView.tableFooterView=nil;
        [self.tableView reloadData];
        
    }else if (type == Ants_ConvertToMyAccount)
    {
        NSDictionary *dic =[pDictionary objectForKey:@"data"];
        [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
    }
    
}

@end
